package com.xf.pub.tool;

import java.io.PrintWriter;
import java.util.Date;

import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.xf.pub.exception.ServiceException;
import com.xf.pub.tool.json.JsonDateValueProcessor;

public class ResponseTools {
	private static ResponseTools tool;
	private Logger logger;
	private static  JsonConfig jsonConfig=new JsonConfig();
	
	
	static {
		jsonConfig.registerJsonValueProcessor(Date.class, new JsonDateValueProcessor()); 
	}
    
	public ResponseTools() {
		this.logger = LoggerFactory.getLogger(super.getClass());
	}
	public static ResponseTools get() {
		if (tool == null)
			tool = new ResponseTools();
		return tool;
	}

	private String buildBizInfo(Object info) {
		JSONObject obj = new JSONObject();
		obj.put("success", Boolean.valueOf(true));
		obj.put("desc", "success");
		obj.put("errorcode", "E0");
		obj.put("info", (info == null) ? "[]" : JSONArray.fromObject(info,jsonConfig));
		return obj.toString();
	}
	private void responseResult(HttpServletResponse resp, String o) {
		PrintWriter pw = null;
		resp.setContentType("text/javascript;charset=utf-8");
		resp.setCharacterEncoding("utf-8");
		String value = o;
		try {
			pw = resp.getWriter();
			pw.write(value.toString());
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			pw.close();
		}
	}

	private String buildError(ServiceException e) {
		JSONObject obj = new JSONObject();
		obj.put("success", Boolean.valueOf(false));
		obj.put("desc", e.getMessage());
		obj.put("errorcode", e.getErrorCode());
		obj.put("info", null);
		return obj.toString();
	}

	private String buildExceError(Exception e) {
		JSONObject obj = new JSONObject();
		obj.put("success", Boolean.valueOf(false));
		obj.put("desc", "system error");
		obj.put("errorcode", "2000001");
		obj.put("info", null);
		this.logger.error("errorinfo:", e);
		return obj.toString();
	}

	private void responseInfo(HttpServletResponse resp, Object info) {
		responseResult(resp, buildBizInfo(info) );
	}

	public void response(HttpServletResponse resp, Object info,Object page) {
		if (info instanceof ServiceException)
			responseError(resp, (ServiceException) info, null);
		else if (info instanceof Exception)
			responseError(resp, null, (Exception) info);
		else
			responseInfo(resp, info);
	}
	
	public void response(HttpServletResponse resp, Object info) {
		if (info instanceof ServiceException)
			responseError(resp, (ServiceException) info, null);
		else if (info instanceof Exception)
			responseError(resp, null, (Exception) info);
		else
			responseInfo(resp, info);
	}

	private void responseError(HttpServletResponse resp, ServiceException e,
			Exception e1) {
		String obj = "";
		if (e != null)
			obj = buildError(e);
		else
			obj = buildExceError(e1);

		responseResult(resp, obj);
	}
}